﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <script type="text/javascript">

        //创建对象类型映射表
        var class2type = {};
        var arrName = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
        for (i in arrName) {
            class2type["[object " + arrName[i] + "]"] = arrName[i].toLowerCase();
        }
        //判断类型的函数
        //null undefined boolean number string
        //array function date regexp error  object
        function type(obj) {
            if (obj == null) {
                return String(obj);
            }
            return typeof obj === "object" || typeof obj === "function" ?
                class2type[Object.prototype.toString.call(obj)] || "object" :
                typeof obj;
        }

        alert(type({}));
        alert(type([]));
    </script>
</body>
</html>
